package cn.tedu.sp04orderservice.service;

import cn.tedu.entity.Item;
import cn.tedu.entity.Order;
import cn.tedu.entity.User;
import cn.tedu.service.OrderService;
import cn.tedu.sp04orderservice.feign.ItemClient;
import cn.tedu.sp04orderservice.feign.UserClient;
import cn.tedu.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class orderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    //http://localhost:8201/asdaf212radsf
    @Override
    public Order getOrder(String orderId) {
        log.warn("获取订单="+orderId);

        //TODO: 远程调用user-service获取用户信息
        JsonResult<User> user = userClient.getUser(555);
        //TODO: 远程调用item-service获取商品信息
        JsonResult<List<Item>> items = itemClient.getItems(orderId);

        Order order = new Order();
        order.setId(orderId);
        order.setItems(items.getData());
        order.setUser(user.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.warn("添加订单="+order);

        //http://localhost:8001/5
        //TODO: 远程调用item-service减少商品库存
        itemClient.decreaseNumber(order.getItems());
        //TODO: 远程调用user-service增加用户积分
        userClient.addScore(8, 1000);
    }
}
